﻿using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using ElectronicWMSA.Comm.ClassFiles;
using ElectronicWMSA.Comm.Forms.Message;

namespace ElectronicWMSA.Warehouse
{
    public partial class Frm_PanDian : Form
    {
        private string strWhere = "";
        public Frm_PanDian()
        {
            InitializeComponent();
            this.pageControl1.PageCurrent = 1;
            this.pageControl1.PageSize = 50;
            this.comboBox1.SelectedIndex = 0;
        }
        private int DBindData()
        {
            Comm.Forms.MsControls.PageData pageData = new Comm.Forms.MsControls.PageData();
            pageData.TableName = "Vlh_PD";
            pageData.PrimaryKey = "Machine";
            pageData.OrderStr = "Machine";
            pageData.PageIndex = this.pageControl1.PageCurrent;
            pageData.PageSize = this.pageControl1.PageSize;
            pageData.QueryCondition = strWhere;
            pageData.QueryFieldName = "*";

            this.pageControl1.bindingSource.DataSource = pageData.QueryDataTable().Tables[0];
            this.pageControl1.bindingNavigator.BindingSource = pageControl1.bindingSource;
            this.dataGridViewUserList.AutoGenerateColumns = false;
            this.dataGridViewUserList.DataSource = this.pageControl1.bindingSource;
            this.dataGridViewUserList.ClearSelection();
            this.dataGridViewUserList.CurrentCell = null;
            return pageData.TotalCount;
        }
        private int pageControl1_EventPaging(Comm.Forms.MsControls.EventPagingArg e)
        {
            return DBindData();

        }
        private void dataGridViewUserList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            SolidBrush b = new SolidBrush(dataGridViewUserList.RowHeadersDefaultCellStyle.ForeColor);
            e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), dataGridViewUserList.DefaultCellStyle.Font, b, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4);
        }
        private void btnUserSearch_Click(object sender, EventArgs e)
        {
            this.strWhere = " 1=1 ";
            if (!string.IsNullOrEmpty(comboBox1.Text.Trim()))
            {
                strWhere += "  AND Statius LIKE '%" + comboBox1.Text.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(txtData.Text.Trim()))
            {
                strWhere += "  AND PData = '" + txtData.Text.Trim() + "'";
            }
            if (!string.IsNullOrEmpty(txt_KW.Text.Trim()))
            {
                strWhere += "  AND KW LIKE '%" + txt_KW.Text.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(txt_Machine.Text.Trim()))
            {
                strWhere += "  AND Machine LIKE '%" + txt_Machine.Text.Trim() + "%'";
            }
            this.pageControl1.Bind();
        }

        #region
        private void linkLabelClose_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Main._Main.CloseTabPages();
        }
        private void linkLabellinkLabelCloseAll_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Main._Main.CloseAllTabPages();
        }
        private void linkLabelCloseElse_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Main._Main.CloseTabPageElse();
        }
        #endregion

        #region 导出
        private void btn_exct_Click(object sender, EventArgs e)
        {
            try
            {
                string sql = "";

                if (strWhere != "")
                {
                    sql = "SELECT  Machine AS 机型,SN AS 序列号,KW AS 库位,PData AS 盘点日期  FROM DB_WMSFIRST.dbo.Vlh_PD WHERE " + strWhere + "  GROUP BY  Machine,SN,KW,PData ";
                }
                else
                {
                    sql = "SELECT  Machine AS 机型,SN AS 序列号,KW AS 库位,PData AS 盘点日期  FROM DB_WMSFIRST.dbo.Vlh_PD  GROUP BY  Machine,SN,KW,PData ";
                }

                DataTable dt = SYDZ.WMS.DBUtility.DbHelperSQL.ExecuteDataTable(sql);

                ExPut.GetNopiExcel(dt);
                MessageClass.right("操作成功！");
            }
            catch (Exception ex)
            {
                ErrorLogLoad.ErrorLogInfo(ex.Message.ToString());

                MessageClass.error("操作失败！");
            }
        }
        #endregion

        private void btnSaveFP_Click(object sender, EventArgs e)
        {
            Frm_PanDianOption frm = new Frm_PanDianOption(0, "", "", "", "", "");

            frm.ShowDialog();

            if (frm.Updateflg)
            {
                this.pageControl1.Bind();
            }
        }

        private string _KW = "";
        private string _JX = "";
        private string _LX = "";
        private string _RQ = "";
        private string _SL = "";
        private void dataGridViewUserList_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            _KW = "";
            _JX = "";
            _LX = "";
            _RQ = "";
            _SL = "";
            if (e.RowIndex > -1)
            {
                if (dataGridViewUserList.CurrentRow.Cells["BK04"].Value.ToString() == "Option")
                {
                    _KW = dataGridViewUserList.CurrentRow.Cells["KW"].Value.ToString();
                    _JX = dataGridViewUserList.CurrentRow.Cells["Machine"].Value.ToString();
                    _LX = dataGridViewUserList.CurrentRow.Cells["BK04"].Value.ToString();
                    _RQ = dataGridViewUserList.CurrentRow.Cells["PData"].Value.ToString();
                    _SL = "0";
                }
                else
                {
                    MessageClass.error("请选择灯泡数据！");
                }
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                if (_LX == "")
                {
                    MessageClass.error("请选择列表数据！");
                    return;
                }
                else if (_LX != "Option")
                {
                    MessageClass.error("请先查询灯泡数据并选择列表！");
                    return;
                }
                else 
                {
                    string strsql = "  SELECT COUNT(*) FROM Vlh_PD WHERE KW='" + _KW + "' AND Machine='" + _JX + "' AND PData='" + _RQ + "' AND BK04='" + _LX + "'";
                    _SL = SYDZ.WMS.DBUtility.DbHelperSQL.Query(strsql).Tables[0].Rows[0][0].ToString();
                    Frm_PanDianOption frm = new Frm_PanDianOption(1, _KW, _JX, _LX, _RQ, _SL);
                    frm.ShowDialog();
                    if (frm.Updateflg)
                    {
                        this.pageControl1.Bind();
                    }
                }
            }
            catch (Exception ex)
            {
                ErrorLogLoad.ErrorLogInfo(ex.Message.ToString());
                MessageClass.error("操作失败！");
            }
        }


    }
}
